﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using WebChart;
using System.Drawing;
using BussinessLogicTecnoPez.Produccion.Enfermedades_y_control_de_estanques;

public partial class Modules_EnfermedadesControlEstanque_ConsultarResultadosSensor : System.Web.UI.Page
{
    private int idSensor
    {
        get { return Convert.ToInt32(ViewState["idSensor"]); }
        set { ViewState["idSensor"] = value; }
    }

    private List<ResultadoSensor> Resultados
    {
        get { return (List<ResultadoSensor>) ViewState["resultados"]; }
        set { ViewState["resultados"] = value; }
    }


    protected void Page_Load(object sender, EventArgs e)
    {
        //if (this.IsPostBack == false)
        //{
        //    this.idSensor = Convert.ToInt32(Request["idsensor"]);
        //    consultarDatos();
        //    Graficar();
        //}

        this.idSensor = Convert.ToInt32(Request["idsensor"]);
        if (this.IsPostBack == false)
        {
            CargarDatosSensor();
        }

        Globals_GenericMasterPageNoSecurity master = (Globals_GenericMasterPageNoSecurity)this.Master;
        master.Imagen = "../Styles/Icons/kchart.png";
        master.Titulo = "Resultados de sensor";
        
        fraGrafico.Attributes.Add("src", "GraficoResultados.aspx?idsensor=" + this.idSensor.ToString());
    }

    private void CargarDatosSensor()
    {
        Sensor.getManager.ClearFilters();
        Sensor.getManager.ClearOrders();
        Sensor.getManager.AddFilter("CODIGO", BussinessLogicTecnoPez.GenericManager<Sensor>.filterOperator.Equals, this.idSensor);
        Sensor objSensor = Sensor.getManager.GetOne();

        lblCodigo.Text = objSensor.Codigo.ToString();
        lblDescripcion.Text = objSensor.Descripcion;
        if (objSensor.ConfiguracionSensor != null)
        {
            lblParametroControl.Text = objSensor.ParametroControl.ToString();
            lblEquipo.Text = objSensor.ConfiguracionSensor.NombreEquipo;
            lblEspecie.Text = objSensor.ConfiguracionSensor.Especie.ToString();
            lblEstanque.Text = objSensor.ConfiguracionSensor.Estanque.ToString();
        }
    }

    private void consultarDatos()
    {
        //ResultadoSensor.getManager.ClearFilters();
        //ResultadoSensor.getManager.ClearOrders();

        //ResultadoSensor.getManager.AddFilter("Sensor", BussinessLogicTecnoPez.GenericManager<ResultadoSensor>.filterOperator.Equals, this.idSensor);
        //ResultadoSensor.getManager.AddOrder("fecha", BussinessLogicTecnoPez.GenericManager<ResultadoSensor>.Order.orderDirection.DESC);

        List<ResultadoSensor> lista = ResultadoSensor.ConsultarUltimosResultados(this.idSensor);

        int contador = 1;
        this.Resultados= new List<ResultadoSensor>();
        foreach (ResultadoSensor item in lista)
        {
            this.Resultados.Insert(0,item);
            contador++;
            if (contador == 10)
            {
                break;
            }
        }
    }

    private void Graficar()
    {
        //WebChart.LineChart chart1 = new LineChart();
        //chart1.Line.Color = Color.Blue;
        //chart1.Fill.Color = Color.Blue;
        //chart1.LineMarker = new DiamondLineMarker(8, Color.Blue, Color.Yellow);
        //chart1.Legend = "Valor";

        //WebChart.LineChart chart2 = new LineChart();
        //chart2.Line.Color = Color.Red;
        //chart2.Fill.Color = Color.Red;
        //chart2.LineMarker = new DiamondLineMarker(8, Color.Red, Color.Yellow);
        //chart2.Legend = "Valor Máximo";

        //WebChart.LineChart chart3 = new LineChart();
        //chart3.Line.Color = Color.Yellow;
        //chart3.Fill.Color = Color.Yellow;
        //chart3.LineMarker = new DiamondLineMarker(8, Color.Yellow, Color.Yellow);
        //chart3.Legend = "Valor Mínimo";

        //foreach (ResultadoSensor item in this.Resultados)
        //{
        //    chart1.Data.Add(new ChartPoint(item.Fecha.ToString(), (float)item.Valor));
        //    chart2.Data.Add(new ChartPoint(item.Fecha.ToString(), (float)item.ValorMaximo));
        //    chart2.Data.Add(new ChartPoint(item.Fecha.ToString(), (float)item.ValorMinimo));
        //}

        //ChartControl1.Charts.Clear();
        //ChartControl1.Charts.Add(chart1);


        //ConfigureColors(ChartControl1);
        //ChartControl1.RedrawChart();

    }
    protected void Timer1_Tick(object sender, EventArgs e)
    {
        consultarDatos();
        Graficar();
    }

    private void ConfigureColors(ChartControl ChartControlToSetup)
    {
        ChartControlToSetup.Background.Color = Color.White;//Color.FromArgb(75, Color.SteelBlue);
        ChartControlToSetup.Background.Type = InteriorType.LinearGradient;
        ChartControlToSetup.Background.ForeColor = Color.SteelBlue;
        ChartControlToSetup.Background.EndPoint = new Point(500, 350);
        ChartControlToSetup.Legend.Position = LegendPosition.Bottom;
        ChartControlToSetup.Legend.Width = 40;

        ChartControlToSetup.YAxisFont.ForeColor = Color.SteelBlue;
        ChartControlToSetup.XAxisFont.ForeColor = Color.SteelBlue;
        //ChartControlToSetup.XAxisFont.Font.Size = 5; 

        ChartControlToSetup.ChartTitle.ForeColor = Color.White;

        ChartControlToSetup.Border.Color = Color.SteelBlue;
        ChartControlToSetup.BorderStyle = BorderStyle.None;

    }
    protected void cmdBuscar_Click(object sender, EventArgs e)
    {
        Response.Redirect("ABMSensores.aspx");
    }
}
